#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTimer>
#include <QPushButton>
#include <QDateTime>

static float num2 = 0;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 启动定时器
    timer_id1 = startTimer(1000);
    timer_id2 = startTimer(2000);

    QFont font;
    font.setPointSize(20);
    ui->label->setFont(font);
    ui->label_2->setFont(font);
    ui->label_3->setFont(font);
    ui->label_4->setFont(font);

    QTimer* timer = new QTimer(this);

    connect(ui->pushButton, &QPushButton::clicked, [=](){
        timer->start(1000);
    });

    connect(timer, &QTimer::timeout, [=](){
        static int num = 1;
        ui->label_3->setText(QString::number(num++));
    });

    connect(timer, &QTimer::timeout, this, &Widget::TimeUpdate);

    connect(ui->pushButton_2, &QPushButton::clicked, [=](){
        timer->stop();
    });
}
Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e)
{

    if(e->timerId() == timer_id1)
    {
        static int num1 = 1;
        if(num1 % 2 == 1)
        {
            num2 += 0.5;
        }

        // label1每一秒加一次
        ui->label->setText(QString::number(num1++));
        qDebug() << "num1:" << num1 << " num2:" << num2;
    }
    if(e->timerId() == timer_id2)
    {
        num2 += 0.5;
        ui->label_2->setText(QString::number(num2));
    }
}

void Widget::TimeUpdate()
{
    QString str = QDateTime::currentDateTime().toString("yyyy-MM-dd hh::mm::ss");
    ui->label_4->setText(str);
}

